﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;
using PagedList;

namespace HdipCricketClub.Controllers
{ 
    public class CountyController : Controller
    {
        private CricketClubEntities db = new CricketClubEntities();

        //
        // GET: /County/

        public ViewResult Index(int? page)
        {
            var counties = db.Counties.OrderBy(c => c.name);

            int pageSize = 10;
            int pageNumber = (page ?? 1);

            return View(counties.ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /County/Details/5

        public ViewResult Details(int id)
        {
            County county = db.Counties.Single(c => c.county_id == id);
            return View(county);
        }

        //
        // GET: /County/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /County/Create

        [HttpPost]
        public ActionResult Create(County county)
        {
            if (ModelState.IsValid)
            {
                db.Counties.AddObject(county);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(county);
        }
        
        //
        // GET: /County/Edit/5
 
        public ActionResult Edit(int id)
        {
            County county = db.Counties.Single(c => c.county_id == id);
            return View(county);
        }

        //
        // POST: /County/Edit/5

        [HttpPost]
        public ActionResult Edit(County county)
        {
            if (ModelState.IsValid)
            {
                db.Counties.Attach(county);
                db.ObjectStateManager.ChangeObjectState(county, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(county);
        }

        //
        // GET: /County/Delete/5
 
        public ActionResult Delete(int id)
        {
            County county = db.Counties.Single(c => c.county_id == id);
            return View(county);
        }

        //
        // POST: /County/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                County county = db.Counties.Single(c => c.county_id == id);
                db.Counties.DeleteObject(county);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                County county = db.Counties.Single(c => c.county_id == id);
                return RedirectToAction("UpdateError", new { id = county.county_id });
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        //
        //  GET:    /County/UpdateError

        public ActionResult UpdateError(int id)
        {
            County county = db.Counties.Single(c => c.county_id == id);

            return View(county);
        }

    }
}